<?php
namespace app\im\model;

use app\BaseModel;

class ImReplyType extends BaseModel
{
	//定义表名称
	protected $name = 'longbing_card_reply_type';
	
	public function searchTitleAttr($query, $value, $data)
    {
        $query->where('title','like', '%' . $value . '%');
    }
    
    public function searchUniacidAttr($query, $value, $data)
    {
        $query->where('uniacid','=', $value );
    }
	
	public function reply()
    {
        return $this->belongsTo('ImMyReply' ,'id' ,'type');
    }
	//创建话术
	public function createType($data)
	{
		$data['create_time'] = time();
		return $this->save($data);
	}
	//更新话术
	public function updateType($filter ,$data)
	{
		$data['update_time'] = time();
		$result =  $this->where($filter)->update($data);
		return !empty($result);
	}
	
	//获取话术列表
	public function listType($filter ,$page_config = ['page' => 1 ,'page_count' => 20])
	{
		$result = $this->withSearch(['title' ,'uniacid'] ,$filter)
			->page($page_config['page'] ,$page_config['page_count'])
			->order('top desc, id desc')
			->select();
		if(!empty($result)) $result = $result->toArray(); 
		return $result;
	} 
	//获取话术列表
	public function getTypeCount($filter)
	{
		$result = $this->withSearch(['title' ,'uniacid'] ,$filter)
			->count();
		if(empty($result)) $result = 0;
		return $result;
	} 
	
	//删除话术
	public function delType($filter)
	{
		$result =  $this->where($filter)->delete();
		return !empty($result);
	}
    
    public function listAllType($filter)
    {
        $result = $this->where($filter)
                       ->order('top' ,'desc')
                       ->select();
        if(empty($result)) $result = $result->toArray();
        return $result;
    }
	
}
